package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.entity.file.AppVersion;
import com.dd.cloud.user.entity.file.FileInfo;
import com.dd.cloud.user.req.file.AppVersionCreateReq;
import com.dd.cloud.user.req.file.AppVersionUpdateReq;
import com.dd.cloud.user.req.file.FilePageReq;
import com.dd.cloud.user.res.file.FileUploadProgressRes;
import com.dd.cloud.user.res.file.HwObsParamRes;
import com.dd.cloud.user.service.admin.IAdminUserService;
import com.dd.cloud.user.service.file.IAppVersionService;
import com.dd.cloud.user.service.file.IFileInfoService;
import com.dd.cloud.common.utils.ResourceDownloadUtil;
import com.dd.cloud.user.util.HuaWeiOBSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IFileInfoService fileInfoService;
    @Autowired
    private IAppVersionService appVersionService;

    /**
     * 机构医生批量导入模板下载
     *
     * @param response
     */
    @GetMapping("/mechan/template")
    public void downloadDoctorExcel(HttpServletResponse response) {
        String fileName = "template/名老中医智能辅助诊疗（学习）系统添加机构信息表.xlsx";
        ResourceDownloadUtil.resourceDownload(response, fileName);
    }

    /**
     * 药房批量导入医生模板下载
     *
     * @param response
     */
    @GetMapping("/doctor/template")
    public void downloadPharmacyDrugsExcel(String client, HttpServletResponse response) {
        fileInfoService.downloadTemplate(client, response);
    }

    /**
     * 中医知识库模板下载
     *
     * @param response
     */
    @GetMapping("/file/template")
    public void downloadPharmacyDrugsExcel(HttpServletResponse response, @NotNull(message = "模板类型不能为空") Integer type) {
        adminUserService.resourceDownload(response, type);
    }


    /**
     * 文件上传, 单个文件 返回对象
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/single")
    public ResponseEntity<FileInfo> uploadFile(@NotNull(message = "文件不能为空") MultipartFile file) {
        FileInfo fileInfo = fileInfoService.uploadSingleFile(file);
        return ResponseEntity.ok(fileInfo);
    }

    /**
     * 文件上传, 单个文件 返回链接
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/single/str")
    public ResponseEntity<String> uploadFileStr(@NotNull(message = "文件不能为空") MultipartFile file) {
        String fileInfo = fileInfoService.uploadSingleFileStr(file);
        return ResponseEntity.ok(fileInfo);
    }

    /**
     * 文件上传, 多个文件
     *
     * @param files
     * @return
     */
    @PostMapping("/upload/multiple")
    public ResponseEntity<List<FileInfo>> uploadFiles(@NotNull(message = "文件列表不能为空") MultipartFile[] files) {
        List<FileInfo> path = fileInfoService.uploadMultipleFile(files);
        return ResponseEntity.ok(path);
    }

    /**
     * 文件上传, 多个文件
     *
     * @param request
     * @return
     */
    @PostMapping("/upload/multiple/uni")
    public ResponseEntity<List<FileInfo>> uploadFilesUni(MultipartHttpServletRequest request) {
        Map<String, MultipartFile> fileMap = request.getFileMap();
        MultipartFile[] files = new MultipartFile[fileMap.size()];
        int i = 0;
        for (MultipartFile item : fileMap.values()) {
            files[i] = item;
            i++;
        }
        List<FileInfo> path = fileInfoService.uploadMultipleFile(files);
        return ResponseEntity.ok(path);
    }

    /**
     * 查看文件上传进度
     *
     * @param request
     * @return
     */
    @GetMapping("/upload/progress")
    public ResponseEntity<FileUploadProgressRes> uploadProgress(HttpServletRequest request, String time) {
        FileUploadProgressRes path = fileInfoService.uploadProgress(request, time);
        return ResponseEntity.ok(path);
    }

    /**
     * 检查app版本
     *
     * @param appId
     * @param version
     * @param type
     * @return
     */
    @GetMapping("/app/version/check")
    public ResponseEntity<AppVersion> checkAppVersion(@NotBlank(message = "appid不能为空") String appId,
                                                      @NotBlank(message = "app版本不能为空") String version,
                                                      @NotNull(message = "app平台不能为空") Integer type) {
        AppVersion appVersion = appVersionService.checkAppVersion(appId, version, type);
        return ResponseEntity.ok(appVersion);
    }

    /**
     * obs对象存储参数
     *
     * @return
     */
    @GetMapping("/obs/param")
    public ResponseEntity<HwObsParamRes> obsParam() {
        HwObsParamRes res = appVersionService.obsParam();
        return ResponseEntity.ok(res);
    }

    /**
     * 分页查询文件列表
     *
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<Page<FileInfo>> filePage(@Valid FilePageReq req) {
        Page<FileInfo> res = fileInfoService.filePage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 添加自定义文件
     *
     * @param fileInfo: 文件内容
     * @return
     */
    @PostMapping("/add")
    public ResponseEntity<Void> addFile(@Valid @RequestBody FileInfo fileInfo) {
        fileInfoService.addFile(fileInfo);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改自定义文件状态
     *
     * @param id:     文件id
     * @param status: 状态
     * @return
     */
    @PostMapping("/updatre/status")
    public ResponseEntity<Void> addFile(@NotNull(message = "id不能为空") Integer id,
                                        @NotNull(message = "状态不能为空") Integer status) {
        fileInfoService.updateFileStatus(id, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 修补上传文件(手动上传华为云)测试使用
     */
    @PostMapping("/upload/test")
    public ResponseEntity<List<FileInfo>> getFileStr(@NotNull(message = "文件列表不能为空") MultipartFile[] files) {
        System.out.println(files.length);
        List<FileInfo> fileInfos = fileInfoService.getFileStr(files);
        return ResponseEntity.ok(fileInfos);
    }

}
